package estore.model;
import java.util.Objects;

public class Product implements Comparable<Product> {

    private String id;      // 商品ID
    private String name;    // 商品名称
    private double price;   // 价格
    private int stock;      // 库存

    public Product(String id, String name, double price, int stock) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.stock = stock;
    }

    // 补充所有必要的 getter 方法（必须为 public，供外部调用）
    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {  // 已添加，用于价格排序
        return price;
    }

    public int getStock() {  // 补充此方法，用于库存排序
        return stock;
    }

    // setter 方法按需添加（可选，根据业务是否需要修改属性）
    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    @Override
    public String toString() {
        return String.format("ID: %-5s | 名称: %-20s | 价格: ¥%-8.2f | 库存: %d",
                id, name, price, stock);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return Objects.equals(id, product.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public int compareTo(Product other) {
        return this.id.compareTo(other.id); // 按商品ID升序排列
    }
}